|
1
|
|
|
'use strict' |
|
2
|
|
|
|
|
3
|
|
|
const jsonfile = require('jsonfile') |
|
4
|
|
|
const mkdirp = require('mkdirp') |
|
5
|
|
|
const path = require('path') |
|
6
|
|
|
const fs = require('fs') |
|
7
|
|
|
|
|
8
|
|
|
var ContainerConfig = {} |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Validate a config object. |
|
12
|
|
|
* |
|
13
|
|
|
* @param {ContainerConfigObject} config |
|
14
|
|
|
* @returns {boolean} |
|
15
|
|
|
*/ |
|
16
|
|
|
ContainerConfig.validate = function (config) { |
|
17
|
|
|
config.containerToken = config.containerToken || config.container_token |
|
18
|
|
|
|
|
19
|
|
|
if ('containerToken' in config) { |
|
20
|
|
|
ContainerConfig.setup(config) |
|
21
|
|
|
return true |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
return false |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Set up the app's globals using config values. |
|
29
|
|
|
* |
|
30
|
|
|
* @param {ContainerConfigObject} config |
|
31
|
|
|
*/ |
|
32
|
|
|
ContainerConfig.setup = function (config) { |
|
33
|
|
|
if ('containerEndpoint' in config) { |
|
34
|
|
|
global.containerEndpoint = config.containerEndpoint |
|
35
|
|
|
} |
|
36
|
|
|
if ('version' in config) { |
|
37
|
|
|
global.apiVersion = config.version |
|
38
|
|
|
} |
|
39
|
|
|
if ('stability' in config) { |
|
40
|
|
|
global.apiStability = config.stability |
|
41
|
|
|
} |
|
42
|
|
|
if ('containerEndpointCheckSSL' in config && !config.containerEndpointCheckSSL) { |
|
43
|
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get the correct (relative) path for a container config file. |
|
49
|
|
|
* |
|
50
|
|
|
* @param {string} name |
|
51
|
|
|
* @returns {string} |
|
52
|
|
|
*/ |
|
53
|
|
|
ContainerConfig.getPath = function (name) { |
|
54
|
|
|
return path.join('.inc', 'containers', name + '.json') |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Read a container config file. If the file with the supplied container name doesn't exist, |
|
59
|
|
|
* this falls back to the 'default' container, if that one exists. |
|
60
|
|
|
* |
|
61
|
|
|
* @param {string} name |
|
62
|
|
|
* @param {function(Error, ContainerConfigObject)} cb |
|
63
|
|
|
*/ |
|
64
|
|
|
ContainerConfig.read = function (name, cb) { |
|
65
|
|
|
jsonfile.readFile(ContainerConfig.getPath(name), function (err, config) { |
|
66
|
|
|
// Container file exists and is a JSON object, so check if it is valid |
|
67
|
|
|
if (!err && typeof config === 'object' && ContainerConfig.validate(config)) { |
|
68
|
|
|
// Yep, return in callback! |
|
69
|
|
|
cb(null, config) |
|
70
|
|
|
return |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Try default file if in `beta` mode |
|
74
|
|
|
if (name === 'beta') { |
|
75
|
|
|
ContainerConfig.read('default', cb) |
|
76
|
|
|
return |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// No valid container found |
|
80
|
|
|
cb(err || new Error('Container config not found or invalid'), null) |
|
81
|
|
|
}) |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Write to the config file for a specific container. |
|
86
|
|
|
* |
|
87
|
|
|
* @param {string} name |
|
88
|
|
|
* @param {object} content |
|
89
|
|
|
*/ |
|
90
|
|
|
ContainerConfig.write = function (name, content) { |
|
91
|
|
|
// Ensure the containers directory exists |
|
92
|
|
|
mkdirp(path.join('.inc', 'containers'), function () { |
|
93
|
|
|
// Write JSON file |
|
94
|
|
|
jsonfile.writeFile(ContainerConfig.getPath(name), content) |
|
95
|
|
|
// Write package version file |
|
96
|
|
|
if (!fs.existsSync(path.join('.inc', 'version'))) { |
|
97
|
|
|
fs.writeFileSync(path.join('.inc', 'version'), global.containerVersion.toString(), 'utf8') |
|
98
|
|
|
} |
|
99
|
|
|
}) |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
module.exports = ContainerConfig |
|
103
|
|
|
|